﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Scribd
{
    public class ScribdConfigControl : AbstractConfigControl
    {
        private ScribdProvider provider;
        private ScribdConfig config;
        private TextBox apiKeyTextBox = new TextBox();
        private TextBox secretKeyTextBox = new TextBox();

        public override string Value
        {
            get
            {
                EnsureChildControls();

                config.ApiKey = apiKeyTextBox.Text;
                config.SecretKey = secretKeyTextBox.Text;

                return config.SerializeToJson();
            }
        }

        public ScribdConfigControl(ScribdProvider provider, string options)
        {
            this.provider = provider;
            this.config = options.DeserializeJsonTo<ScribdConfig>();
        }

        protected override void OnLoad(EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                apiKeyTextBox.Text = config.ApiKey;
                secretKeyTextBox.Text = config.SecretKey;
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            apiKeyTextBox.ID = "apiKeyTextBox";
            secretKeyTextBox.ID = "secretKeyTextBox";

            apiKeyTextBox.CssClass = "guiInputText guiInputStandardSize";
            secretKeyTextBox.CssClass = "guiInputText guiInputStandardSize";

            Controls.Add(apiKeyTextBox);
            Controls.Add(secretKeyTextBox);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddFormRow("API Key:", "Please enter a valid Scribd API key", apiKeyTextBox);
            writer.AddFormRow("Secret Key:", "Please enter your Scribd Secret key", secretKeyTextBox);
        }
    }
}